import os
from google.appengine.ext.webapp import template

import cgi
import urllib

from google.appengine.api import users
from google.appengine.api import urlfetch
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

class WaveUser(db.Model):
  name = db.StringProperty(required=True)
  waveId = db.StringProperty(required=True)
  mobile = db.PhoneNumberProperty()
  fixeline = db.PhoneNumberProperty()

class MainPage(webapp.RequestHandler):
  def get(self):
    wavers_query = WaveUser.all()
    wavers = wavers_query.fetch(100)

    template_values = {'wavers': wavers}

    path = os.path.join(os.path.dirname(__file__), 'index.html')
    self.response.out.write(template.render(path, template_values))

class adduser(webapp.RequestHandler):
  def post(self):
    w_name = self.request.get('name')
    w_Id = self.request.get('id')
    w_phone = self.request.get('phone')
    w_mobile = self.request.get('mobile')

    wavers_query = WaveUser.all()
    wavers_query.filter('waveId=',w_Id)
    waver = wavers_query.get()
    if waver:
      waver.name = w_name
      waver.fixeline = w_phone
      waver.mobile = w_mobile
    else:
      waver = WaveUser( name     = w_name,
                        waveId   = w_Id,
                        fixeline = w_phone,
                        mobile   = w_mobile)
    waver.put()
    self.redirect('/')
    
application = webapp.WSGIApplication(
                                     [('/', MainPage),
                                      ('/add', adduser)],
                                     debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()